package com.zy.experience_system.controller;

import com.zy.experience_system.enums.PermsEnum;
import com.zy.experience_system.enums.ResultEnum;
import com.zy.experience_system.enums.RoleEnum;
import com.zy.experience_system.exception.CommonException;
import com.zy.experience_system.form.*;
import com.zy.experience_system.service.StuHomeworkService;
import com.zy.experience_system.util.ResultUtil;
import com.zy.experience_system.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/homework")
public class HomeworkController extends BaseController {

    @Autowired
    private StuHomeworkService stuHomeworkService;

    @PostMapping("/create")
    public ResultVO create(@Validated @RequestBody SetAndStuForm setAndStuForm) throws Exception {
        try {
            stuHomeworkService.save(setAndStuForm);
            return ResultUtil.success(null);
        } catch (Exception e) {
            return ResultUtil.error("创建失败");
        }
    }

    @GetMapping("/showFromSet")
    public ResultVO showFromSet(@Validated @RequestBody SetAndStuForm setAndStuForm) throws Exception {
        try {
            List<HomeworkSimpleVO> homeworkSimpleVOList = stuHomeworkService.findAllByStuIdAndSetId(setAndStuForm);
            return ResultUtil.success(homeworkSimpleVOList);
        } catch (Exception e) {
            return ResultUtil.error("创建失败");
        }
    }

    @PutMapping("/check")
    public ResultVO check(@Validated @RequestBody CheckHomeworkForm checkHomeworkForm) throws Exception {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            try {
                stuHomeworkService.saveScore(checkHomeworkForm);
                return ResultUtil.success(null);
            } catch (Exception e) {
                return ResultUtil.error("创建失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);

    }

    @GetMapping("/show/{homeworkId}")
    public ResultVO show(@PathVariable("homeworkId") Integer homeworkId) throws Exception {
        try {
            StuHomeworkVO stuHomeworkVO = stuHomeworkService.findById(homeworkId);
            return ResultUtil.success(stuHomeworkVO);
        } catch (Exception e) {
            return ResultUtil.error("查询失败");
        }
    }

    @GetMapping("/showByTest")
    public ResultVO showByTest(@Validated @RequestBody TestAndSetNoTeachForm testAndSetNoTeachForm) throws Exception {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            try {
                TeachHomeworkVO teachHomeworkVO = stuHomeworkService.findByTestIdAndSetId(testAndSetNoTeachForm);
                return ResultUtil.success(teachHomeworkVO);
            } catch (Exception e) {
                return ResultUtil.error("查询失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @PutMapping("/stuUpdate")
    public ResultVO stuUpdate(@Validated @RequestBody HomeworkAnswerForm homeworkAnswerForm) throws Exception {
        try {
            stuHomeworkService.saveAnswer(homeworkAnswerForm);
            return ResultUtil.success(null);
        } catch (Exception e) {
            return ResultUtil.error("提交失败");
        }
    }

    @GetMapping("/showTestNumList/{setId}")
    public ResultVO showTestNumList(@PathVariable("setId") Integer setId) throws Exception {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            try {
                List<TeachTestListVO> teachTestListVOList = stuHomeworkService.findBySetId(setId);
                return ResultUtil.success(teachTestListVOList);
            } catch (Exception e) {
//                System.out.println(e.toString());
                return ResultUtil.error("查询失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @GetMapping("/showOrder/{setId}")
    public ResultVO showOrder(@PathVariable("setId") Integer setId) throws Exception {
        try {
            List<StuOrderVO> stuOrderVOList = stuHomeworkService.findOrderBySetId(setId);
            return ResultUtil.success(stuOrderVOList);
        } catch (Exception e) {
            return ResultUtil.error("查询失败");
        }
    }

}
